home *** CD-ROM | disk | FTP | other *** search
/ Inside Mac Games Volume 5 #3 / IMG 46 Vol 5-3.iso / More Goodies / More For Your Game / Realmz / Character Master Source / Nemesis Framework / Sources / nemesis files.cpp < prev    next >
Text File  |  1996-07-03  |  14KB  |  742 lines

  1. //•••••••••••••••••••••••••••••••••••••••••
  2. //    Some functions to read data from files
  3. //•••••••••••••••••••••••••••••••••••••••••
  4.  
  5. OSErr    NemesisReadData( FSSpec fileSpec, long offset, long numBytes, Handle &buffer )
  6. {
  7.     OSErr    error = noErr;
  8.     short    fileID;
  9.     
  10.     error = FSpOpenDF( &fileSpec, fsRdPerm, &fileID );
  11.     if( error )
  12.     {
  13.         fileID = 0;
  14.         return error;
  15.     }
  16.     
  17.     error = SetFPos( fileID, fsFromStart, offset );
  18.     if( error )
  19.     {
  20.         FSClose( fileID );
  21.         return error;
  22.     }
  23.             
  24.     error = FSRead( fileID, &numBytes, *buffer );
  25.     if( error )
  26.     {
  27.         FSClose( fileID );
  28.         return error;
  29.     }
  30.  
  31.     FSClose( fileID );
  32.     
  33.     return error;
  34. }
  35.  
  36. OSErr    NemesisReadOneByte( FSSpec fileSpec, long offset, SInt8 &theValue )
  37. {
  38.     OSErr    error = noErr;
  39.     Handle    buffer;
  40.  
  41.     error = NemesisAllocateHandle( kOneByte, &buffer );
  42.     if( error )
  43.     {
  44.         theValue = 0;
  45.         return error;
  46.     }
  47.  
  48.     NemesisLockHandle( buffer );
  49.     
  50.     error = NemesisReadData( fileSpec, offset, kOneByte, buffer );
  51.     if( error )
  52.     {
  53.         theValue = 0;
  54.         return error;
  55.     }
  56.  
  57.     theValue = **buffer;
  58.     NemesisDisposeHandle( &buffer );
  59.     
  60.     return error;
  61. }
  62.  
  63. OSErr    NemesisReadTwoBytes( FSSpec fileSpec, long offset, SInt16 &theValue )
  64. {
  65.     OSErr    error = noErr;
  66.     Handle    buffer;
  67.  
  68.     error = NemesisAllocateHandle( kTwoBytes, &buffer );
  69.     if( error )
  70.     {
  71.         theValue = 0;
  72.         return error;
  73.     }
  74.  
  75.     NemesisLockHandle( buffer );
  76.     
  77.     error = NemesisReadData( fileSpec, offset, kTwoBytes, buffer );
  78.     if( error )
  79.     {
  80.         theValue = 0;
  81.         return error;
  82.     }
  83.  
  84.     theValue = (SInt16 &)**buffer;
  85.     NemesisDisposeHandle( &buffer );
  86.     
  87.     return error;
  88. }
  89.  
  90. OSErr    NemesisReadFourBytes( FSSpec fileSpec, long offset, SInt32 &theValue )
  91. {
  92.     OSErr    error = noErr;
  93.     Handle    buffer;
  94.  
  95.     error = NemesisAllocateHandle( kFourBytes, &buffer );
  96.     if( error )
  97.     {
  98.         theValue = 0;
  99.         return error;
  100.     }
  101.  
  102.     NemesisLockHandle( buffer );
  103.     
  104.     error = NemesisReadData( fileSpec, offset, kFourBytes, buffer );
  105.     if( error )
  106.     {
  107.         theValue = 0;
  108.         return error;
  109.     }
  110.  
  111.     theValue = (SInt32 &)**buffer;
  112.     NemesisDisposeHandle( &buffer );
  113.     
  114.     return error;
  115. }
  116.  
  117. OSErr    NemesisReadCString( FSSpec fileSpec, long offset, long maxBytes, char *theString )
  118. {
  119.     OSErr    error = noErr;
  120.     Handle    buffer;
  121.     short    fileID;
  122.     long    count;
  123.     Boolean    EOString;
  124.     long    tempLong;
  125.     
  126.     error = FSpOpenDF( &fileSpec, fsRdPerm, &fileID );
  127.     if( error )
  128.     {
  129.         theString[0] = (char)0;    // Nullify string…
  130.         fileID = 0;
  131.         return error;
  132.     }
  133.     
  134.     error = NemesisAllocateHandle( 1, &buffer );
  135.     if( error )
  136.     {
  137.         theString[0] = (char)0;    // Nullify string…
  138.         FSClose( fileID );
  139.         return error;
  140.     }
  141.         
  142.     NemesisLockHandle( buffer );
  143.     
  144.     error = SetFPos( fileID, fsFromStart, offset );
  145.     if( error )
  146.     {
  147.         theString[0] = (char)0;    // Nullify string…
  148.         FSClose( fileID );
  149.         return error;
  150.     }
  151.     
  152.     EOString = false;
  153.     count = 0;
  154.     do
  155.     {    
  156.         tempLong = 1;    // For FSRead
  157.         error = FSRead( fileID, &tempLong, *buffer );
  158.         if( error )    // Check for errors and close file and exit if there was one
  159.         {
  160.             theString[count] = (char)0;    // Put null at end of string.
  161.             FSClose( fileID );
  162.             return error;
  163.         }
  164.         theString[count] = (char)**buffer;    // Add character to string.
  165.         if( (count >= maxBytes) || (theString[count]==0) )    // Check for end
  166.         {
  167.             EOString = true;
  168.             theString[count] = 0;    // Make sure last character is null
  169.         }
  170.         else
  171.             EOString = false;
  172.         
  173.         ++count;
  174.     }while( !EOString );
  175.     
  176.     FSClose( fileID );
  177.     NemesisDisposeHandle( &buffer );
  178.     
  179.     return error;
  180. }
  181.  
  182. OSErr    NemesisReadPString( FSSpec fileSpec, long offset, long maxBytes, Str255 theString )
  183. {
  184.     OSErr    error = noErr;
  185.     Handle    buffer;
  186.     short    fileID;
  187.     long    count;
  188.     long    tempLong;
  189.     
  190.     if( maxBytes > 256 )
  191.         maxBytes = 256;    // 256 is max number of chars for a pascal string (length+string)
  192.     
  193.     error = FSpOpenDF( &fileSpec, fsRdPerm, &fileID );
  194.     if( error )
  195.     {
  196.         theString[0] = (char)0;    // Nullify string…
  197.         fileID = 0;
  198.         return error;
  199.     }
  200.     
  201.     error = NemesisAllocateHandle( 1, &buffer );
  202.     if( error )
  203.     {
  204.         theString[0] = (char)0;    // Nullify string…
  205.         FSClose( fileID );
  206.         return error;
  207.     }
  208.         
  209.     NemesisLockHandle( buffer );
  210.     
  211.     error = SetFPos( fileID, fsFromStart, offset );
  212.     if( error )
  213.     {
  214.         theString[0] = (char)0;    // Nullify string…
  215.         FSClose( fileID );
  216.         return error;
  217.     }
  218.     
  219.     // Check how long the string is…
  220.     tempLong = 1;    // For FSRead
  221.     error = FSRead( fileID, &tempLong, *buffer );
  222.     if( error )    // Check for errors and close file and exit if there was one
  223.     {
  224.         theString[0] = 0;    // Nullify string…
  225.         FSClose( fileID );
  226.         return error;
  227.     }
  228.     
  229.     // Set max length of string
  230.     tempLong = (long)**buffer;
  231.     
  232.     if( tempLong < maxBytes )
  233.         maxBytes = tempLong;
  234.     
  235.     theString[0] = (char)maxBytes;
  236.     
  237.     // Loop till we have read it all…
  238.     // (Remeber to start from 1 'cos we have already read the first
  239.     //  byte and the position marker has moved on)
  240.     for( count = 1; count <= maxBytes; count++ )
  241.     {    
  242.         tempLong = 1;    // For FSRead
  243.         error = FSRead( fileID, &tempLong, *buffer );
  244.         if( error )    // Check for errors and close file and exit if there was one
  245.         {
  246.             theString[0] = (count-1);    // Put number of chars at beginning of string.
  247.             FSClose( fileID );
  248.             return error;
  249.         }
  250.         theString[count] = (char)**buffer;    // Add character to string.
  251.     }
  252.     
  253.     FSClose( fileID );
  254.     NemesisDisposeHandle( &buffer );
  255.     
  256.     return error;
  257. }
  258.  
  259. OSErr    NemesisCreateTempFile( FSSpec realFile, FSSpec &tempFile, OSType theCreator, OSType theType )
  260. {
  261.     OSErr        error = noErr;
  262.     Str255         tempFileName;
  263.     short         tempVRefNum;
  264.     long         tempFileID;
  265.     unsigned long seconds;
  266.     Handle        buffer;
  267.     short        fileID;
  268.     long        fileSize = 0;
  269.     long        tempFSize = 0;
  270.  
  271.     /* make up a temporary filename */
  272.     GetDateTime( &seconds );
  273.     NumToString( seconds, tempFileName );
  274.  
  275.     /* find the temporary folder, create it if necessary */
  276.     /* temp file and original must be on same volume, or FSpExchangeFiles() chokes */
  277.     error = FindFolder( realFile.vRefNum,
  278.                         kTemporaryFolderType,
  279.                         kCreateFolder,
  280.                         &tempVRefNum,
  281.                         &tempFileID);
  282.     if (error)
  283.     {
  284.         return error;
  285.     }
  286.     
  287.     /* make an FSSpec for the temporary filename */
  288.     error = FSMakeFSSpec( tempVRefNum,
  289.                           tempFileID,
  290.                           tempFileName,
  291.                           &tempFile);
  292.     if (error == fnfErr)    /* if the file is not found */
  293.     {
  294.         error = noErr;     /* not a problem, it doesn't exist yet */
  295.     }
  296.     
  297.     // Now create a duplicate file
  298.     error = FSpCreate( &tempFile, theCreator, theType, smSystemScript );
  299.     if( error )
  300.     {
  301.         return error;
  302.     }
  303.  
  304.     error = FSpOpenDF( &realFile, fsRdPerm, &fileID );
  305.     if( error )
  306.     {
  307.         fileID = 0;
  308.         return error;
  309.     }
  310.     
  311.     error = GetEOF( fileID, &fileSize );
  312.     if( error )
  313.     {
  314.         FSClose( fileID );
  315.         return error;
  316.     }
  317.     
  318.     tempFSize = fileSize;    // tempFSize is needed for FSWrite
  319.     
  320.     error = NemesisAllocateHandle( fileSize, &buffer );
  321.     if( error )
  322.     {
  323.         FSClose( fileID );
  324.         return error;
  325.     }
  326.         
  327.     NemesisLockHandle( buffer );
  328.     
  329.     error = SetFPos( fileID, fsFromStart, 0 );
  330.     if( error )
  331.     {
  332.         FSClose( fileID );
  333.         return error;
  334.     }
  335.         
  336.     error = FSRead( fileID, &fileSize, *buffer );
  337.     if( error )
  338.     {
  339.         FSClose( fileID );
  340.         return error;
  341.     }
  342.     
  343.     FSClose( fileID );    // Close original file
  344.     
  345.     // Now to write the data to the temporary file
  346.     error = FSpOpenDF( &tempFile, fsRdWrPerm, &fileID );
  347.     if( error )
  348.     {
  349.         fileID = 0;
  350.         return error;
  351.     }
  352.     
  353.     error = SetFPos( fileID, fsFromStart, 0 );
  354.     if( error )
  355.     {
  356.         FSClose( fileID );
  357.         return error;
  358.     }
  359.     
  360.     error = FSWrite( fileID, &tempFSize, *buffer );
  361.     if( error )
  362.     {
  363.         FSClose( fileID );
  364.     }
  365.     else
  366.     {
  367.         error = FSClose( fileID );
  368.         if( !error )
  369.         {
  370.             error = FlushVol( nil, tempFile.vRefNum );
  371.         }
  372.     }
  373.     
  374.     // Release the buffer
  375.     NemesisDisposeHandle( &buffer );
  376.  
  377.     return error;
  378. }
  379.  
  380. OSErr    NemesisDeleteTempFile( FSSpec &tempFile, FSSpec &origFile )
  381. {
  382.     OSErr    error = noErr;
  383.     
  384.     error = FSpExchangeFiles( &tempFile, &origFile );
  385.     if( !error )
  386.     {
  387.         error = FSpDelete( &tempFile );
  388.     }
  389.     
  390.     return error;
  391. }
  392.  
  393. OSErr    NemesisWriteData( FSSpec &fileSpec, long offset, long numBytes, const Handle buffer )
  394. {
  395.     OSErr    error = noErr;
  396.     short    fileID;
  397.         
  398.     // Open file
  399.     error = FSpOpenDF( &fileSpec, fsRdWrPerm, &fileID );
  400.     if( error )
  401.     {
  402.         fileID = 0;
  403.         return error;
  404.     }
  405.         
  406.     error = SetFPos( fileID, fsFromStart, offset );
  407.     if( error )
  408.     {
  409.         FSClose( fileID );
  410.         return error;
  411.     }
  412.         
  413.     error = FSWrite( fileID, &numBytes, *buffer );
  414.     if( error )
  415.     {
  416.         FSClose( fileID );
  417.     }
  418.     else
  419.     {
  420.         error = FSClose( fileID );
  421.     }
  422.     
  423.     return error;
  424. }
  425.  
  426. OSErr    NemesisWrite1Data( FSSpec &fileSpec, long offset, long numBytes, const SInt8 **buffer )
  427. {
  428.     OSErr    error = noErr;
  429.     short    fileID;
  430.         
  431.     // Open file
  432.     error = FSpOpenDF( &fileSpec, fsRdWrPerm, &fileID );
  433.     if( error )
  434.     {
  435.         fileID = 0;
  436.         return error;
  437.     }
  438.         
  439.     error = SetFPos( fileID, fsFromStart, offset );
  440.     if( error )
  441.     {
  442.         FSClose( fileID );
  443.         return error;
  444.     }
  445.         
  446.     error = FSWrite( fileID, &numBytes, *buffer );
  447.     if( error )
  448.     {
  449.         FSClose( fileID );
  450.     }
  451.     else
  452.     {
  453.         error = FSClose( fileID );
  454.     }
  455.     
  456.     return error;
  457. }
  458.  
  459. OSErr    NemesisWrite2Data( FSSpec &fileSpec, long offset, long numBytes, const SInt16 **buffer )
  460. {
  461.     OSErr    error = noErr;
  462.     short    fileID;
  463.         
  464.     // Open file
  465.     error = FSpOpenDF( &fileSpec, fsRdWrPerm, &fileID );
  466.     if( error )
  467.     {
  468.         fileID = 0;
  469.         return error;
  470.     }
  471.         
  472.     error = SetFPos( fileID, fsFromStart, offset );
  473.     if( error )
  474.     {
  475.         FSClose( fileID );
  476.         return error;
  477.     }
  478.         
  479.     error = FSWrite( fileID, &numBytes, *buffer );
  480.     if( error )
  481.     {
  482.         FSClose( fileID );
  483.     }
  484.     else
  485.     {
  486.         error = FSClose( fileID );
  487.     }
  488.     
  489.     return error;
  490. }
  491.  
  492. OSErr    NemesisWrite4Data( FSSpec &fileSpec, long offset, long numBytes, const SInt32 **buffer )
  493. {
  494.     OSErr    error = noErr;
  495.     short    fileID;
  496.         
  497.     // Open file
  498.     error = FSpOpenDF( &fileSpec, fsRdWrPerm, &fileID );
  499.     if( error )
  500.     {
  501.         fileID = 0;
  502.         return error;
  503.     }
  504.         
  505.     error = SetFPos( fileID, fsFromStart, offset );
  506.     if( error )
  507.     {
  508.         FSClose( fileID );
  509.         return error;
  510.     }
  511.         
  512.     error = FSWrite( fileID, &numBytes, *buffer );
  513.     if( error )
  514.     {
  515.         FSClose( fileID );
  516.     }
  517.     else
  518.     {
  519.         error = FSClose( fileID );
  520.     }
  521.     
  522.     return error;
  523. }
  524.  
  525. OSErr    NemesisWriteOneByte( FSSpec fileSpec, long offset, SInt8 theValue )
  526. {
  527.     OSErr    error = noErr;
  528.     SInt8 **buffer;
  529.  
  530.     error = NemesisAllocateHandle( kOneByte, (Handle *)&buffer );
  531.     if( error )
  532.     {
  533.         return error;
  534.     }
  535.  
  536.     NemesisLockHandle( (Handle)buffer );
  537.     **buffer = theValue;
  538.     
  539.     error = NemesisWrite1Data( fileSpec, offset, kOneByte, buffer );
  540.     if( error )
  541.     {
  542.         return error;
  543.     }
  544.  
  545.     NemesisDisposeHandle( (Handle *)&buffer );
  546.     
  547.     return error;
  548. }
  549.  
  550. OSErr    NemesisWriteTwoBytes( FSSpec fileSpec, long offset, SInt16 theValue )
  551. {
  552.     OSErr    error = noErr;
  553.     SInt16**buffer;
  554.  
  555.     error = NemesisAllocateHandle( kTwoBytes, (Handle *)&buffer );
  556.     if( error )
  557.     {
  558.         return error;
  559.     }
  560.  
  561.     NemesisLockHandle( (Handle)buffer );
  562.     **buffer = theValue;
  563.     
  564.     error = NemesisWrite2Data( fileSpec, offset, kTwoBytes, buffer);
  565.     if( error )
  566.     {
  567.         return error;
  568.     }
  569.  
  570.     NemesisDisposeHandle( (Handle *)&buffer );
  571.     
  572.     return error;
  573. }
  574.  
  575. OSErr    NemesisWriteFourBytes( FSSpec fileSpec, long offset, SInt32 theValue )
  576. {
  577.     OSErr    error = noErr;
  578.     SInt32**buffer;
  579.  
  580.     error = NemesisAllocateHandle( kFourBytes, (Handle *)&buffer );
  581.     if( error )
  582.     {
  583.         return error;
  584.     }
  585.  
  586.     NemesisLockHandle( (Handle)buffer );
  587.     **buffer = theValue;
  588.     
  589.     error = NemesisWrite4Data( fileSpec, offset, kFourBytes, buffer );
  590.     if( error )
  591.     {
  592.         return error;
  593.     }
  594.  
  595.     NemesisDisposeHandle( (Handle *)&buffer );
  596.     
  597.     return error;
  598. }
  599.  
  600.  
  601. OSErr    NemesisWriteCString( FSSpec fileSpec, long offset, long maxBytes, const char *theString )
  602. {
  603.     OSErr    error = noErr;
  604.     Handle    buffer;
  605.     short    fileID;
  606.     long    count;
  607.     Boolean    EOString;
  608.     long    tempLong;
  609.     
  610.     error = FSpOpenDF( &fileSpec, fsRdPerm, &fileID );
  611.     if( error )
  612.     {
  613.         fileID = 0;
  614.         return error;
  615.     }
  616.     
  617.     error = NemesisAllocateHandle( 1, &buffer );
  618.     if( error )
  619.     {
  620.         FSClose( fileID );
  621.         return error;
  622.     }
  623.         
  624.     NemesisLockHandle( buffer );
  625.     
  626.     error = SetFPos( fileID, fsFromStart, offset );
  627.     if( error )
  628.     {
  629.         FSClose( fileID );
  630.         return error;
  631.     }
  632.     
  633.     EOString = false;
  634.     count = 0;
  635.     do
  636.     {    
  637.         **buffer = theString[count];
  638.         tempLong = 1;    // For FSWrite
  639.         error = FSWrite( fileID, &tempLong, *buffer );
  640.         if( error )    // Check for errors and close file and exit if there was one
  641.         {
  642.             FSClose( fileID );
  643.             return error;
  644.         }
  645.         if( (count >= maxBytes) || (theString[count]==0) )    // Check for end
  646.         {
  647.             EOString = true;
  648.             **buffer = (char)0;
  649.             error = FSWrite( fileID, &tempLong, *buffer ); // Make EOS null
  650.             if( error )    // Check for errors and close file and exit if there was one
  651.             {
  652.                 FSClose( fileID );
  653.                 return error;
  654.             }
  655.         }
  656.         else
  657.             EOString = false;
  658.         
  659.         ++count;
  660.     }while( !EOString );
  661.     
  662.     FSClose( fileID );
  663.     NemesisDisposeHandle( &buffer );
  664.     
  665.     return error;
  666. }
  667.  
  668. OSErr    NemesisWritePString( FSSpec fileSpec, long offset, long maxBytes, const Str255 theString )
  669. {
  670.     OSErr    error = noErr;
  671.     Handle    buffer;
  672.     short    fileID;
  673.     long    count;
  674.     long    tempLong;
  675.     
  676.     if( maxBytes > 256 )
  677.         maxBytes = 256;    // 256 is max number of chars for a pascal string (length+string)
  678.     
  679.     error = FSpOpenDF( &fileSpec, fsRdPerm, &fileID );
  680.     if( error )
  681.     {
  682.         fileID = 0;
  683.         return error;
  684.     }
  685.     
  686.     error = NemesisAllocateHandle( 1, &buffer );
  687.     if( error )
  688.     {
  689.         FSClose( fileID );
  690.         return error;
  691.     }
  692.         
  693.     NemesisLockHandle( buffer );
  694.     
  695.     error = SetFPos( fileID, fsFromStart, offset );
  696.     if( error )
  697.     {
  698.         FSClose( fileID );
  699.         return error;
  700.     }
  701.     
  702.     // Check how long the string is…
  703.     tempLong = (long)theString[0];
  704.     
  705.     if( tempLong < maxBytes )
  706.         maxBytes = tempLong;
  707.         
  708.     // Loop till we have written it all…
  709.     for( count = 0; count <= maxBytes; count++ )
  710.     {    
  711.         tempLong = 1;    // For FSWrite
  712.         **buffer = theString[count];
  713.         error = FSWrite( fileID, &tempLong, *buffer );
  714.         if( error )    // Check for errors and close file and exit if there was one
  715.         {
  716.             FSClose( fileID );
  717.             return error;
  718.         }
  719.     }
  720.     
  721.     FSClose( fileID );
  722.     NemesisDisposeHandle( &buffer );
  723.     
  724.     return error;
  725. }
  726.  
  727. Boolean    NemesisSameFileSpec( const FSSpec &fileOne, const FSSpec &fileTwo)
  728. {
  729.     Boolean result = false;
  730.     
  731.     if ( (fileOne.vRefNum != fileTwo.vRefNum)    // Check volumes
  732.           || (fileOne.parID != fileTwo.parID))    // Check directories
  733.     {
  734.         return result;    // Not the same
  735.     }
  736.     
  737.     // Now check the name    
  738.     result = (EqualString(fileOne.name, fileTwo.name, false, true));
  739.     
  740.     return result;
  741. }
  742.